package com.qf.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.rowset.BaseRowSet;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitMQConfigTTL {

    public static String busExchange = "bus-exchange";
    public static String busQueue = "bus-queue";
    public static String busRoute = "bus.*";

    public static String dlxExchange = "dlx-exchange";
    public static String dlxQueue = "dlx-queue";
    public static String dlxRouter = "dlx.*";

    private static  String maxQueue = "max-queue";

    @Bean
    public TopicExchange busExchange() {
        return new TopicExchange(busExchange, true, false, null);
    }

    @Bean
    public TopicExchange dlxExchange() {
        return new TopicExchange(dlxExchange, true, false, null);
    }

    @Bean
    public Queue busQueue() {
        // 定义一个Map，在Map中封装死信的路哟建，然后给业务队列
        Map<String, Object> args = new HashMap();
        args.put("x-dead-letter-exchange", dlxExchange); // 死信的交换机
        args.put("x-dead-letter-routing-key", "dlx.error"); // 死信路由键
        args.put("x-message-ttl",1000*10); // 队列中消息超过这个时间后就会变成死信
        return new Queue(busQueue, true, false, false, args);
    }
    @Bean
    public Queue maxQueue() {
        Map<String, Object> args = new HashMap();
        args.put("x-max-length",10);
        args.put("x-dead-letter-exchange", dlxExchange); // 死信的交换机
        args.put("x-dead-letter-routing-key", "dlx.error"); // 死信路由键
        return new Queue(maxQueue, true, false, false, args);
    }

    @Bean
    public Queue dlxQueue() {
        return new Queue(dlxQueue, true, true, false, null);
    }

    @Bean
    public Binding busQueueBind() {
        return BindingBuilder.bind(busQueue()).to(busExchange()).with(busRoute);
    }

    @Bean
    public Binding dlxQueueBind() {
        return BindingBuilder.bind(dlxQueue()).to(dlxExchange()).with(dlxRouter);
    }

    @Bean
    public Binding maxQueueBind() {
        return BindingBuilder.bind(maxQueue()).to(busExchange()).with("max.*");
    }


}
